package exam.controller;


import exam.bean.PermissionGroupInfo;
import exam.service.IPermissionGroupService;
import exam.service.IPermissionRelationService;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/permissionGroup")
public class PermissionGroupController extends BaseController{

    @Autowired
    IPermissionGroupService permissionGroupService;

    @Autowired
    IPermissionRelationService permissionRelationService;


    @PreAuthorize("hasAuthority('sys:permissions:list')")
    @GetMapping("/info")
    public Msg permissionGroupInfo(){
        return Msg.success().add("info",permissionGroupService.selectInfo());
    }


    @PostMapping("/update")
    public Msg update(@RequestBody PermissionGroupInfo permissionGroupInfo){
        boolean re = permissionGroupService.updateInfo(permissionGroupInfo) > 0;
        return Msg.success().setMessage(re ? "更新成功" : "更新失败");
    }

    @GetMapping("/delete")
    public Msg delete(@RequestParam(value = "id",defaultValue = "") int id){
        if(id == 10){
            return Msg.fail().setMessage("默认分组不可删除");
        }else if(id == 1){
            return Msg.fail().setMessage("超级管理员权限组禁止删除");
        }else{
            boolean re = permissionGroupService.deleteInfo(id) > 0;
            //删除缓存
            try {
                groupAndMenuInfoService.clearUserAuthorityInfoByMenuId(id);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            //删除权限组与权限关联表数据
            permissionRelationService.deleteInfo(id);
            return Msg.success().setMessage(re ? "删除成功" : "删除失败");
        }
    }

    @PostMapping("/insertInfo")
    public Msg insetInfo(@RequestBody PermissionGroupInfo permissionGroupInfo){
        boolean re =permissionGroupService.insertInfo(permissionGroupInfo) > 0;
        return Msg.success().setMessage(re?"添加成功" : "添加失败");
    }

    @GetMapping("/detail")
    public Msg detail(@RequestParam(value = "id" ,defaultValue = "") int id){
        return Msg.success().add("permissionGroupInfo",permissionGroupService.selectInfoById(id));
    }

    /**
     * 更改权限组权限
     * @param groupId   权限组id
     * @param ids       权限id组
     */
    @PostMapping("/permissionGroup/{groupId}")
    public Msg permissionEdit(@PathVariable("groupId") Integer groupId,@RequestBody Integer[] ids ){

        if(groupId == 10){
            return Msg.fail().setMessage("默认分组权限不可更改");
        }

        //删除原有数据
        permissionRelationService.deleteInfo(groupId);

        List<Integer> relations = new ArrayList<>(Arrays.asList(ids));
//        for (Integer it : relations) {
//            System.out.print(it + " ");
//        }
        //多条数据添加
        boolean re = permissionRelationService.insertInfoOfList(relations,groupId) > 0 ;

        //删除当前用户权限缓存
        try{
            groupAndMenuInfoService.clearUserAuthorityInfoByMenuId(groupId);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }


        return Msg.success().setMessage(re ? "权限分配成功,重新登录后生效！" : "权限分配失败，请稍后再试！").add("menuId",ids);
    }


    /**
     * 根据权限组id获取其所对应的权限id
     * @param id    权限组id
     */
    @GetMapping("/getPermissionGroupOfPerm")
    public Msg getPermissionGroupPerm(@RequestParam(value = "id",defaultValue = "") Integer id){
        return Msg.success().add("permId",permissionRelationService.getPerIdOfByGroupId(id));
    }

}
